﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace FreedomX2.Core.IO
{
    public static class ConvertorEx
    {
        public static string ConvertToString(this ArrayList al)
        {
            return Convertor.ConvertObjectsToString(al.ToArray());
        }
        /// <summary>
        /// 从XML或XML文件转换为DataSet
        /// </summary>
        /// <param name="xmlData"></param>
        /// <returns></returns>
        public static DataSet ConvertXMLToDataSet(this string xmlData)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmlData);
                //从stream装载到XmlTextReader
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                return xmlDS;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null) reader.Close();
            }
        }
    }
    public class Convertor : IDisposable
    {
        private static List<MemoryStream> msList = new List<MemoryStream>();
        /// <summary>
        /// 读取为byte[]
        /// </summary>
        /// <typeparam name="T">各种继承自Stream的类</typeparam>
        /// <param name="t">被读取的实体</param>
        /// <returns></returns>
        public static byte[] ReadToBytes<T>(T t) where T : Stream
        {
            try
            {
                byte[] bytes = new byte[t.Length];
                t.Read(bytes, 0, bytes.Length);
                t.Flush();
                t.Close();
                return bytes;
            }
            catch { }
            return null;
        }
        public static Stream WriteToStream(byte[] buffer)
        {
            Stream reStream = null;

            try
            {
                MemoryStream ms = new MemoryStream(buffer);
                msList.Add(ms);
                reStream = (Stream)ms;
            }
            catch { }

            return reStream;
        }
        public static string ConvertObjectsToString(object[] objects)
        {
            return ConvertBytesToString(ConvertObjectsToBytes(objects));
        }
        public static byte[] ConvertObjectsToBytes(object[] objects)
        {
            byte[] bytes = new byte[objects.Length];
            int i = 0;
            foreach (var v in objects) { bytes[i] = (byte)v; i++; }
            return bytes;
        }
        public static string ConvertBytesToString(byte[] bytes)
        {
            string str = System.Text.Encoding.Default.GetString(bytes);
            return str;
        }
        public static byte[] ConvertStringToBytes(string strVal)
        {
            byte[] byteArray = System.Text.Encoding.Default.GetBytes(strVal);
            return byteArray;
        }

        /// <summary>
        /// 从XML或XML文件转换为DataSet
        /// </summary>
        /// <param name="xmlFilePath"></param>
        /// <returns></returns>
        public static DataSet ConvertXMLToDataSet(string xmlFilePath)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                XmlDocument xmld = new XmlDocument();
                xmld.Load(xmlFilePath.ToString());

                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmld.InnerXml);
                //从stream装载到XmlTextReader
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                //xmlDS.ReadXml(xmlFile);
                return xmlDS;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null) reader.Close();
            }
        }

        /// <summary>
        /// 将DataSet转换为XML或XML文件
        /// </summary>
        /// <param name="xmlDS"></param>
        /// <returns></returns>
        public static string ConvertDataSetToXML(DataSet xmlDS)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;

            try
            {
                stream = new MemoryStream();
                //从stream装载到XmlTextReader
                writer = new XmlTextWriter(stream, Encoding.Unicode);

                //用WriteXml方法写入文件.
                xmlDS.WriteXml(writer);
                int count = (int)stream.Length;
                byte[] arr = new byte[count];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, count);

                UnicodeEncoding utf = new UnicodeEncoding();
                return utf.GetString(arr).Trim();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }

        /// <summary>
        /// 将DataSet转换为XML或XML文件
        /// </summary>
        /// <param name="xmlDS"></param>
        /// <param name="xmlFile"></param>
        public static void ConvertDataSetToXML(DataSet xmlDS, string xmlFilePath)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;

            try
            {
                stream = new MemoryStream();
                //从stream装载到XmlTextReader
                writer = new XmlTextWriter(stream, Encoding.Unicode);

                //用WriteXml方法写入文件.
                xmlDS.WriteXml(writer);
                int count = (int)stream.Length;
                byte[] arr = new byte[count];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, count);

                //返回Unicode编码的文本
                UnicodeEncoding utf = new UnicodeEncoding();
                StreamWriter sw = new StreamWriter(xmlFilePath.ToString());
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sw.WriteLine(utf.GetString(arr).Trim());
                sw.Close();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }

        public void Dispose()
        {
            msList.ForEach(a =>
            {
                a.Flush();
                a.Close();
                a = null;
            });
            msList = new List<MemoryStream>();
        }


    }
}
